<?php

namespace App\Service;

use App\Models\EwNodesAuth;
use App\Models\EwNodes;
use App\Models\EwRoles;
class NodesAuthService extends BaseService
{
	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns('DISTINCT(a.id) AS id,a.node_id,n.name as nodes_name,r.role_name,a.role_id,a.create_time');
		$builder->addFrom("App\Models\EwNodesAuth", 'a');
		$builder->leftJoin("App\Models\EwNodes", 'a.node_id = n.id' , 'n');
		$builder->leftJoin("App\Models\EwRoles", 'a.role_id = r.id' , 'r');

		//nodes_name
		if (isset($params->nodes_name) && $params->nodes_name)
			$builder->andwhere("n.nodes_name LIKE :nodes_name:")->bind(['nodes_name' => $params->nodes_name]);

		//role_name
		if (isset($params->role_name) && $params->role_name)
			$builder->andwhere("r.role_name LIKE :role_name:")->bind(['role_name' => $params->role_name]);
		
		if ($count){
			return $builder->getQuery()->execute()->count();
		}else{
			$builder->limit($params->page_num,($params->p-1)*$params->page_num);
			$result = $builder->getQuery();
			return $result->execute();
		}
	}

	public static function getInfo($id = 0)
	{
		return EwNodesAuth::findFirst($id);
	}

	public static function count($params = NULL)
	{
		return EwNodesAuth::count();
	}

	/**
	 * [checkAuth 检查访问权限]
	 * @param  [type] $role_id [角色id]
	 * @param  [type] $api_url [访问url]
	 * @return [type]          [description]
	 */
	public static function checkAuth($role_id, $api_url)
	{
		//根据用户所属角色，查找该角色配置的菜单节点来判断访问权限-李仁伟
        return self::getAccessAuth($role_id, $api_url);

        /*
        $info = EwNodes::findFirst(['columns'=>'id','conditions'=>'api_url = "'. $api_url . '"']);

		if (!$info)
			return FALSE;

		$node_id = $info->id;
		$raw = EwNodesAuth::findFirst(['conditions'=>'node_id = ' . $node_id . ' AND role_id = ' . $role_id ]);
		if (!$raw)
			return FALSE;

		return TRUE;
        */
	}

	/**
	 * [save 保存数据]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$row = new EwNodesAuth;
		if (isset($data->id) && $data->id)
			$row = EwNodesAuth::findFirst($data->id);

		return $row->save((array)$data);
	}

	/**
	 * [delete 彻底删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwNodesAuth::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}
    /**
     * [getAccessAuth ]
     * @param  [type] $role_id [description]
     * * @param  [type] $api_url [description]
     * @return true 有权限  [description]
     */
    public static function getAccessAuth($role_id, $api_url){
        return TRUE;
        /*
        $nodes=EwNodes::find(['columns'=>'id','conditions'=>"api_url='{$api_url}' "])->toArray();
        if (!$nodes)
            return FALSE;
        $info=EwRoles::find(['columns'=>'menu_nodes','conditions'=>"id={$role_id} and FIND_IN_SET({$nodes[0]['id']},menu_nodes) "])->toArray();
        if (!$info)
            return FALSE;
        return TRUE;
        */
    }
}